package com._520it.crm.web.controller;

import com._520it.crm.domain.OrderBill;
import com._520it.crm.page.AjaxResult;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.OrderBillQueryObject;
import com._520it.crm.service.IOrderBillService;
import com._520it.crm.service.IProductService;
import com._520it.crm.util.Premium;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/orderBill")
public class OrderBillController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IOrderBillService orderBillService;

    @RequestMapping("")
    public String index() {
        return "orderBill/orderBill";
    }

    //新增保单第一页
    @RequestMapping("index1")
    @ResponseBody
    public Long index1(OrderBill ob, Model model) {
        orderBillService.saveData1(ob);
        model.addAttribute("orderBillId", ob.getId());
        return ob.getId();
    }

    //新增保单第二页的查询客户
    @RequestMapping("client")
    public String index2() {
        return "clientAdd";
    }

    //新增保单第二页
    @RequestMapping("orderBill1")
    public String index3(Long id, Model model) {
        model.addAttribute("obId", id);
        OrderBill ob = orderBillService.selectById(id);
        model.addAttribute("insureBillNumber",ob.getInsureBillNumber());
        model.addAttribute("carNumber",ob.getCarPlateNumber());
        return "orderBill/orderBill1";
    }

    @RequestMapping("orderBill2")
    @ResponseBody
    public AjaxResult index4(Long orderBillId,OrderBill ob,boolean IsequalRec,boolean equalOwnerId){
        ob.setId(orderBillId);
        System.out.println(orderBillId);
        System.out.println(IsequalRec);
        System.out.println(equalOwnerId);
        System.err.println(ob);
        AjaxResult result = null;
        try {
            orderBillService.insertData(ob, IsequalRec, equalOwnerId);
            result = new AjaxResult(true, "1");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult("保存失败");
        }
        return result;
    }




    @RequestMapping("save")
    @ResponseBody
    public AjaxResult save(OrderBill ob) {
        AjaxResult result = null;
        try {
            orderBillService.insert(ob);
            result = new AjaxResult(true,"保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult("保存失败");
        }
        return result;
    }

    //保费计算
    @RequestMapping("/selectPremium")
    @ResponseBody
    public Premium selectPremium(Long id) {
        return orderBillService.selectPremiumByBillId(id);
    }

    @RequestMapping("selectByOrderBillId")
    @ResponseBody
    public OrderBill selectByOrderBillId(Long orderBillId){
        OrderBill result = null;
        try {
            result = orderBillService.selectByPrimaryKey(orderBillId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    //申请核保
    @RequestMapping("apply")
    @ResponseBody
    public AjaxResult apply(Long id){
        AjaxResult result = null;
        try {
            orderBillService.updateById(id);
            result = new AjaxResult(true,"申请成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult("申请失败");
        }
        return  result;
    }

    @RequestMapping("selectBill")
    @ResponseBody
    public PageResult selectBill(OrderBillQueryObject qo){
        PageResult result = null;
        try {
            result = orderBillService.selectBill(qo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/orderBillUpdate")
    public String orderBillUpdate(){
        return "/orderBill/orderBillUpdate";
    }

    //高级查询
    @RequestMapping("selectBillAdvanced")
    @ResponseBody
    public PageResult selectBillAdvanced(OrderBillQueryObject qo){
        PageResult result = null;
        qo.setOrderBillStatus(-1);
        try {
            result = orderBillService.selectBill(qo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //打开暂存单界面
    @RequestMapping("/temp")
    public String temp() {
        return "orderBill/orderBillAlter";
    }

    //根据id软删除保单
    @RequestMapping("remove")
    @ResponseBody
    public AjaxResult remove(Long orderBillId){
        AjaxResult result = null;
        try {
            orderBillService.removeById(orderBillId,OrderBill.STATUS_YES,OrderBill.EXPIRED);
            result = new AjaxResult(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult("删除失败");
        }
        return  result;
    }

    //跳转到修改页面
    @RequestMapping("/modify")
    public String revise(Long orderBillId, Model model){
        //把保单id带到编辑页面
        model.addAttribute("orderBillId",orderBillId);
        return "orderBill/orderBillAlter1";
    }
    @RequestMapping("/pending_approval_data")
    @ResponseBody
    public PageResult dataList(OrderBillQueryObject qo){
        PageResult result = null;
        result = orderBillService.queryDataList(qo);
        return result;
    }

    @RequestMapping("alter")
    public String alter(){
        return "orderBill/orderBillAlter";
    }


    //更新状态
    @RequestMapping("alter1")
    @ResponseBody
    public AjaxResult alter1(OrderBill ob,Long stauts){
        AjaxResult result = null;
        try {
            if(stauts != null){
                orderBillService.updateById(ob.getId());
            }
            orderBillService.updateTime(ob);
            result = new AjaxResult(true,"保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult("保存失败");
        }

        return result;
    }
}
